package org.example.studyonline.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.example.studyonline.mapper.ProductMapper;
import org.example.studyonline.mapper.PurchaseRecordMapper;
import org.example.studyonline.mapper.UserMapper;
import org.example.studyonline.pojo.Product;
import org.example.studyonline.pojo.PurchaseRecord;
import org.example.studyonline.pojo.User;
import org.example.studyonline.service.ProductService;
import org.example.studyonline.service.UserService;
import org.example.studyonline.utils.AuthUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public  class ProductServiceImpl implements ProductService {
    @Resource
    private ProductMapper productMapper;

    @Resource
    private PurchaseRecordMapper purchaseRecordMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public Boolean pay(Integer productID) {
        Float price = productMapper.selectById(productID).getPrice();
        String type = productMapper.selectById(productID).getType();
        if(type.equals("VIP")){
            User user = new User();
            user.setUserID(AuthUtils.getUserID());
            user.setIsVIP(1);

            userMapper.updateById(user);
        }

        PurchaseRecord newRecord = new PurchaseRecord();
        newRecord.setPrice(price);
        newRecord.setProductID(productID);
        newRecord.setPurchaseTime(LocalDateTime.now());
        newRecord.setUserID(AuthUtils.getUserID());

        if (purchaseRecordMapper.insert(newRecord) > 0)
            return true;
        else
            return false;

    }

    public List<Product> getVIPproduct() {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type","VIP");

        List<Product> list = productMapper.selectList(queryWrapper);

        return list;

    }
}
